package com.example.aop;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yanzhao
 * @date 2022/6/2 17:03
 * @since 1.0.0
 */
public class Demo {

    public static void main(String[] args) {
        Map<String, Object> map = new ConcurrentHashMap<>();
        ProxyFactory proxyFactory = new ProxyFactory(map);

        proxyFactory.addAdvice(new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                if ("put".equals(method.getName()) && args.length == 2) {
                    System.out.printf("[MethodBeforeAdvice] 当前存放是 Key: %s , Value : %s ，" +
                            "代理对象：%s\n", args[0], args[1], target);
                }
            }
        });

        proxyFactory.addAdvice(new AfterReturningAdvice() {
            @Override
            public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
                if ("put".equals(method.getName()) && args.length == 2) {
                    System.out.printf("[AfterReturningAdvice] 当前存放是 Key: %s , Value : %s ，" +
                            "代理对象：%s\n", args[0], args[1], target);
                }
            }
        });

        proxyFactory.addAdvice(new ThrowsAdvice() {

            public void afterThrowing(Method method, Object[] args, Object target, Exception e) {
                System.out.printf("[ThrowsAdvice] Method : %s , args : %s , target : %s, exception : %s\n",
                        method,
                        Arrays.asList(args),
                        target,
                        e
                );
            }
        });

        Map<String, Object> proxy = proxyFactory.getProxy();
        proxy.put("A", 1);
        proxy.put("B", 2);
        proxy.put(null, 3);

    }
}
